/**
 * 
 */
package com.geoboard.service;

import java.util.ArrayList;

import android.graphics.Point;
import android.view.View;

import com.geoboard.fragments.MapFrag;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;

/**
 * As the board creation markers are dragged, this listener redraws the rectangle btw the markers.
 *
 */
public class MarkerDragListener implements OnMarkerDragListener {
	
	MapFrag mMapFrag;
	GoogleMap mMap;
	Polygon rect;
	Marker mMarker1;
	Marker mMarker2;
	
	public MarkerDragListener(MapFrag mapFrag) {
		this.mMapFrag = mapFrag;
		this.mMap = mapFrag.getMap();
		
		View mapView = mapFrag.getView();
		int quarterHeight = (mapView.getHeight()+1)/4;
		int quarterWidth = (mapView.getWidth()+1)/4;
		Point rectTopLeft = new Point(mapView.getLeft() + quarterWidth,
									mapView.getTop() + quarterHeight);
		Point rectBottomRight = new Point(mapView.getRight() - quarterWidth,
									mapView.getBottom() - quarterHeight);
		
	    mMarker1 = mMap.addMarker(new MarkerOptions()
	    	.position(mMap.getProjection().fromScreenLocation(rectTopLeft))
	    	.draggable(true));
	    mMarker2 = mMap.addMarker(new MarkerOptions()
	    	.position(mMap.getProjection().fromScreenLocation(rectBottomRight))
	    	.draggable(true));
	    
		drawRectangle();
	}

	@Override
	public void onMarkerDrag(Marker marker) {
		drawRectangle();
	}
	
	@Override
	public void onMarkerDragStart(Marker marker) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMarkerDragEnd(Marker marker) {
		// TODO Auto-generated method stub
		
	}
	
	
	/** draw rectangle between two board-creation markers
	 * 
	 */
	public void drawRectangle() {
		
		if (mMarker1 != null && mMarker2 != null) {
			LatLng p1 = mMarker1.getPosition();
			LatLng p2 = mMarker2.getPosition();
			
			//TODO less clippy drawing?
			//add rectangle for first time
			if (rect == null) {
				PolygonOptions rectOptions = new PolygonOptions()
				.add(p1, 
						new LatLng(p1.latitude, p2.longitude),
						p2,
						new LatLng(p2.latitude, p1.longitude))
				.fillColor(0x880000FF)
				.strokeWidth(.1f);
				rect = mMap.addPolygon(rectOptions);
			} else {
				ArrayList <LatLng> points = new ArrayList<LatLng>();
				points.add(p1);
				points.add(new LatLng(p1.latitude, p2.longitude));
				points.add(p2);
				points.add(new LatLng(p2.latitude, p1.longitude));
				rect.setPoints(points);
			}
		}
	}
	
	public Marker[] getBoardCreationMarkers() {
		Marker[] markerArray = {mMarker1, mMarker2};
		return markerArray;
	}
	
	public void destroy() {
		if (rect != null)
			rect.remove();
		if(mMarker1 != null)
			mMarker1.remove();
		if(mMarker2 != null)
			mMarker2.remove();
	}
}
